package com.kitchen.ui.controls.friends
{
	import com.kitchen.KitchenEvent;
	import com.kitchen.com.VerticalScrollBar;
	import com.kitchen.model.Vo.AccountVo;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.MouseEvent;

	public class List extends Sprite
	{
		public var h:Number;
		public var w:Number;
		private var mainSkin:BitmapData;
		private var shoper:Object;
		private var shop:Object;
		private var sbox:ScrollBox;
		private var totalItem:int;
		private var totalPage:int;
		private var pageIndex:int;
		private var pageData:Array;
		
		public function List(mainSkin:BitmapData, shoper:Object, shop:Object)
		{
			super();
			this.mainSkin = mainSkin;
			this.shop = shop;
			this.shoper = shoper;
			this.pageData = new Array();
			this.layout();
		}
		
		private function layout():void{
			this.w = 160;
			this.h = 266;
			var scroll:VerticalScrollBar = new VerticalScrollBar(this.h - 50, this.mainSkin);
			scroll.addEventListener(KitchenEvent.SCROLL, scrollHandle);
			scroll.y = 28;
			scroll.x = this.w - scroll.w;
			this.addChild(scroll);
			var pageItem:Array = new Array();
			this.totalItem = 88;
			this.pageIndex = 1;
			this.totalPage = Math.ceil(this.totalItem/10);
			this.sbox = new ScrollBox(this.mainSkin);
			this.sbox.addEventListener(MouseEvent.CLICK, visitHandle);
			this.addChild(sbox);
			this.sbox.addPage(1, this.getPageItem(1));
			this.sbox.addPage(2, this.getPageItem(2));
		}
		
		private function scrollHandle(ke:KitchenEvent):void{
			var rate:Number = ke.target.rate;
			var ps:int = Math.floor(rate*(this.totalItem-6));
			this.sbox.scroll(ps);
			var cuPage:int = Math.floor(ps/10)+1;
			if(cuPage != this.pageIndex){
				var addPages:Array = new Array(cuPage, cuPage-1, cuPage+1);
				var remPage:Array = new Array(this.pageIndex-1, this.pageIndex+1, this.pageIndex);
				for each(var p:int in addPages){
					if((p>0) && (p <= this.totalPage)){
						this.sbox.addPage(p, this.getPageItem(p));
					}
				}
				for each(var r:int in remPage){
					if(addPages.indexOf(r) == -1){
						this.sbox.removePage(r);
					}
				}
				this.pageIndex = cuPage;
			}
		}
		
		private function visitHandle(ke:MouseEvent):void{
			this.dispatchEvent(ke);
		}
		
		private function getPageItem(page:int):Array{
			if(this.pageData[page] == null){
				var i:int = (page-1)*10+1;
				var end:int = i+10;
				var pageItem:Array = new Array();
				for(; i<end; i++){
					pageItem.push({'odnum':i, 'uid':i*10, 'nick':'神舟'+i+'号', 'kitchen':'3,2,1', 'relate':i%5, 'photo':'/static/images/50.jpg'});
				}
				if(this.pageData.length > 20){
					this.pageData = null;
					this.pageData = new Array();
				}
				this.pageData[page] = pageItem;
			}
			return this.pageData[page];
		}
		
	}
}